package com.ruoyi.cs.base.customer.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cs.base.customer.domain.CustomerInfo;
import com.ruoyi.cs.base.customer.service.ICustomerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 客户信息管理
 * @author Drj 2021/6/28
 */
@RestController
@RequestMapping("/cs/customer")
@Api(value = "客户信息模块", tags = "客户信息接口")
public class CustomerInfoController extends BaseController {
    @Autowired
    private ICustomerInfoService customerInfoService;

    /**
     * 获取客户信息
     * @param customerInfo
     * @return
     */
    @ApiOperationSupport(includeParameters = {"CustomerInfo.sortId", "CustomerInfo.customerInfoId", "CustomerInfo.createTime"})
    @ApiOperation(value = "获取客户信息列表",notes = "查询所有客户信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody CustomerInfo customerInfo) {
        startPage();
        List<CustomerInfo> customerInfos = customerInfoService.selectCustomerInfoList(customerInfo);
        return getDataTable(customerInfos);
    }

    /**
     * 导出客户信息
     *
     * @param customerInfo
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody CustomerInfo customerInfo) {
        List<CustomerInfo> customerInfos = customerInfoService.selectCustomerInfoList(customerInfo);
        ExcelUtil<CustomerInfo> util = new ExcelUtil<CustomerInfo>(CustomerInfo.class);
        return util.exportExcel(customerInfos, "客户信息单数据");
    }

    /**
     * 新增客户信息
     * @param customerInfo 客户信息单据
     * @return
     */
    @ApiOperation(value = "新增客户信息", notes = "新增客户信息")
    @ApiImplicitParam(name = "customerInfo", value = "新增客户信息", dataType = "CustomerInfo")
    @PostMapping("/add")
    public AjaxResult add( @RequestBody CustomerInfo customerInfo) {
        try {
            customerInfoService.insertCustomerInfo(customerInfo);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新客户信息
     * @param customerInfo 客户信息单据
     * @return
     */
    @ApiOperation(value = "更新客户信息", notes = "更新客户信息")
    @ApiImplicitParam(name = "customerInfo", value = "更新客户信息", dataType = "CustomerInfo")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody CustomerInfo customerInfo) {
        try {
            customerInfoService.updateCustomerInfo(customerInfo);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除客户信息
     * 仅修改删除标志位
     *
     * @param customerInfo 客户信息单据
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody CustomerInfo customerInfo) {

        return toAjax(customerInfoService.updateCustomerInfoDeleteFlag(customerInfo));
    }


}
